<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE html>
<html>
  <head>
    <title>教材吧-订单中心</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<%@ include file="/base/jsp/includeweb.jsp"%>
    <link type="text/css" rel="stylesheet" href="${path}/base/css/order.css"/>
  </head>
  <body>
  <%@ include file="/base/jsp/header.jsp"%>
  <%@ include file="/base/jsp/search-header.jsp"%>
  <div id="content-wrapper" class="content-wrapper border-top-org clear">
    <h2>我的订单</h2>
    <div id="cart-info-wrapper">
       
    </div>
    <%@ include file="/base/jsp/page.jsp"%>
  </div>
  <%@ include file="/base/jsp/footer.jsp"%>
  </body>
  <script type="text/javascript">
   var username = "${sessionScope.account}";
   var userid = "${sessionScope.userid}";
   var pageIndex = 0;
   var pageSize = 5;
   var pageT = 0;
   $(function(){
  
    getOrders(pageSize,true);
     $(".pre-page").click(function(){
		        if(pageIndex == 0){
		            alert("当前已经是第一页");
		            return;
		        }
		        pageIndex--;
		        $(".page-wrapper a").each(function(){
		            $(this).removeClass("now");
		            if($(this).attr("name") == "page-"+pageIndex){
		                $(this).addClass("now");
		            }
		        });
		        getOrders(pageSize,true);
		    });
		    
		$(".next-page").click(function(){
		        if(pageIndex == pageT-1){
		            alert("当前已经是最后一页");
		            return;
		        }
		        pageIndex++;
		        $(".page-wrapper a").each(function(){
		            $(this).removeClass("now");
		            if($(this).attr("name") == "page-"+pageIndex){
		                $(this).addClass("now");
		            }
		        })
		         getOrders(pageSize,true);
		   });
   
     
     $("#buy-num-minus").click(function(){
         var num = parseInt($("#number").text());
         if(num > 0){
             $("#number").text(num-1);
         }
     })
     
       
     $("#buy-num-plus").click(function(){
         var num = parseInt($("#number").text());
         $("#number").text(num+1);
     })
     
     $("#cart-but").click(function(){
         
     })
     
     $("#buy-but").click(function(){
         var num = parseInt($("#number").text());
         if(num <= 0){
             alert("请先选择合适的数目");
             return;
         }
     })
  })
  
  function getOrders(pageSize,append){
      $(".page-wrapper").hide();
      request("listOrder.do","userid="+userid+"&isNotPage=0"+"&pageIndex="+pageIndex+"&pageSize="+pageSize,function(data){
          var orders = data.data;
          $(".cart").remove();
          for(var i = 0 ; i　< orders.length; i++){
              var o = orders[i];
              var name = o.username;
              var phone = o.phone;
              var status = o.status;
              var items = orders[i].items;
              for(var k = 0; k < items.length; k++){
                  var item = items[k];
                  var row = $('<div class="cart clear" id="" itemid=""><div class="img-info"><a target="_blank"><img/></a></div>'
                  +'<div class="name-info"><p class="name"><a target="_blank"></a></p></div>'
                  +'<div class="order-info"><p class="orderId">订单号：<span></span></p><p class="time">时间：<span></span></p></div>'
                  +'<div class="order-info"><p class="name">姓名：<span></span></p><p class="phone">电话：<span></span></p></div>'
                  +'<div class="order-price-info"><p class="price"><span></span>元/本</p></div>'
                  +'<div class="order-price-info"><p class="count"><span></span>本</p></div>'
                  +'<div class="order-price-info"><p class="total"><span></span>元</p></div>'
                  +'<div class="cancel-info"><input class="del-but" type="button" value="删除"/><input class="cancel-but" type="button" value="取消"/>&nbsp;&nbsp;&nbsp;<input class="confirm-but" type="button" value="确认收货"/><span class="status-process">处理中</span></div></div>');
                  row.attr("id",o.id);
                  row.attr("itemid",item.id);
                  row.find(".img-info a").attr("href","bookdetail.do?id="+item.bookid);
                  row.find(".img-info a img").attr("src",getImg(item.bookImg));
                  row.find(".name-info a").attr("href","bookdetail.do?id="+item.bookid);
                  row.find(".name-info a").text(item.bookName);
                  row.find(".orderId span").text(item.orderid);
                  row.find(".time span").text($.formatDate("yyyy-MM-dd",item.createTime));
                  row.find(".name span").text(name);
                  row.find(".phone span").text(phone);
                  row.find(".order-price-info .price span").text(item.price);
                  row.find(".order-price-info .count span").text(item.count);
                  row.find(".order-price-info .total span").text(item.count * item.price);
                  if(status == 1 || status == 2){
                      row.find(".cancel-but").remove();
                      row.find(".confirm-but").remove();
                      row.find(".status-process").remove();
                      if(status == 1)
                          row.find(".cancel-info").append($("<span class='status-cancel'>已取消</span>"));
                      else 
                          row.find(".cancel-info").append($("<span class='status-ok'>已完成</span>"));
                  }else{
                      row.find(".del-but").remove();
                      if(status != 3){
                          row.find(".status-process").remove();
                      }
                  }
                  $("#cart-info-wrapper").append(row);
              }
          }
          var total = data.total;
		//计算总页数
		pageT = Math.ceil(total / pageSize);
		//对分页进行处理
		if(pageT > 1){
		    $(".page-wrapper").show();
		    $(".page-num").remove();
		    for(var i = 1 ; i <= pageT ; i++){
		        var row = $('<a class="page-num" href="#"></a>');
		        row.text(i);
		        row.attr("name","page-"+(i-1));
		        row.insertBefore($(".next-page"));
		        if(pageIndex == (i-1)){
		           row.addClass("now");
		        }
		    }
		    $(".page-num").click(function(){
		        if(!$(this).hasClass("now")){
		            pageIndex = parseInt($(this).text())-1;
		            getOrders(pageSize,true);
		         }
		    });
		}else{
		    $(".page-wrapper").hide();
		}
        $(".cancel-but").click(function(){
              if(confirm("确定要取消订单么？")){
                  var orderid = $(this).parents(".cart").attr("id");
                  request("cancelOrder.do","userid="+userid+"&id="+orderid,function(data){
                     if(data.success==true){
                         alert("取消成功");
                         getOrders(pageSize,true);
                     }else{
                         alert("提交失败");
                     }
                  });
              }
          })
          //确认收货的按钮
          $(".confirm-but").click(function(){
              if(confirm("确定收货么？")){
                  var orderid = $(this).parents(".cart").attr("id");
                  request("finishOrder.do","userid="+userid+"&id="+orderid,function(data){
                     if(data.success==true){
                         alert("确认成功");
                         getOrders(pageSize,true);
                     }else{
                         alert("提交失败");
                     }
                  });
              }
          })
          //删除订单的按钮
           $(".del-but").click(function(){
              if(confirm("确定要删除订单么？")){
                  var orderid = $(this).parents(".cart").attr("id");
                  request("delOrder.do","userid="+userid+"&id="+orderid,function(data){
                     if(data.success==true){
                         alert("删除成功");
                         getOrders(pageSize,true);
                     }else{
                         alert("删除失败");
                     }
                  });
              }
          });
      });
  }
  </script>
</html>